﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceWarranty;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceWarrantyRecordItemService
    {
        public InsuranceWarrantyRecordItem LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public bool Exist(long id)
        {
            return LoadEntities(o => o.ID == id).Any();
        }

        public bool DeleteBy(long iwrID, List<long> notDeleteIWRItemIds, string opreatUser,string userDisPlayName)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceWarrantyRecordItem>();
            expressionWhere = expressionWhere.And(o => o.InsuranceWarrantyRecordId == iwrID);
            expressionWhere = expressionWhere.And(o => !notDeleteIWRItemIds.Contains(o.ID));
            var toBeDelList = LoadEntities(expressionWhere).ToList();
            var result = false;
            toBeDelList.ForEach(o =>
            {
                #region 先记录日志
                var opreateLogs = new OpreateLogs
                  {
                      CreatedTime = DateTime.Now,
                      LogTableName = "InsuranceWarrantyRecord",
                      LogSubject = "案件处理信息-删除",
                      LogTablePKID = iwrID,
                      OpreateUserAccount = opreatUser,
                      OpreateUserName = userDisPlayName
                  };
                ServiceFactory.OpreateLogsServiceInstance.AddOpreateLogs(opreateLogs, string.Empty, o, OpreateType.Delete);
                #endregion
                result = DeleteEntities(o);
            });
            return result;
        }

        public IQueryable<InsuranceWarrantyRecordItem> GetPagingList(InsuranceWarrantyRecordItemModel model, out int total)
        {
            var whereExpression = PredicateExtensions.True<InsuranceWarrantyRecordItem>();
            return LoadPagerEntities<InsuranceWarrantyRecordItem>(model.PageIndex, model.iDisplayLength, out total,
               whereExpression, model.iSorts);
        }

        public IQueryable<InsuranceWarrantyRecordItem> LoadByInsuranceWarrantyRecordId(long insuranceWarrantyRecordId)
        {
            return LoadEntities(o => o.InsuranceWarrantyRecordId == insuranceWarrantyRecordId);
        }

    }
}
